package com.sx.zongzhi.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.sx.zongzhi.BaseActivity;
import com.sx.zongzhi.R;
import com.sx.zongzhi.bean.UserBean;
import com.sx.zongzhi.presenter.UpdatePersonInfoPest;
import com.sx.zongzhi.utils.ToastUtils;
import com.sx.zongzhi.voiceamr.UpdatePersonView;
import com.sx.zongzhi.widget.ClearEditText;
import com.sx.zongzhi.widget.LoadDialog;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import xiaofei.library.datastorage.DataStorageFactory;
import xiaofei.library.datastorage.IDataStorage;

/**
 * 密码设置
 * Created by admin on 2017/9/21.
 */

public class PasswordSetActivity extends BaseActivity implements UpdatePersonView {
    @BindView(R.id.et_OldPwd)
    ClearEditText et_OldPwd;
    @BindView(R.id.et_NewPwd)
    ClearEditText et_NewPwd;
    @BindView(R.id.et_reNewPwd)
    ClearEditText etReNewPwd;

    @Override
    public void setUI(Bundle savedInstanceState) {
        setContentView(R.layout.activity_password_set);
        ButterKnife.bind(this);
        setGoneTitle();
       // setTitleName("密码设置");
    }

    @OnClick({R.id.login_btn})
    public void onClicked(View view) {
        switch (view.getId()) {
            case R.id.login_btn:
                submit();
                break;
        }
    }

    private String newPwd;

    private void submit() {
        String oldPwd = et_OldPwd.getText().toString().trim();
        if (TextUtils.isEmpty(oldPwd)) {
            ToastUtils.showToast(this, "请输入旧密码");
            return;
        }
        newPwd = et_NewPwd.getText().toString().trim();
        if (TextUtils.isEmpty(newPwd)) {
            et_NewPwd.setError("新密码不能为空");
            return;
        }
        if (oldPwd.equals(newPwd)) {
            ToastUtils.showToast(this, "两次密码一致,请重新输入密码");
            return;
        }
        if (newPwd.length() > 15 || newPwd.length() < 6) {
            ToastUtils.showToast(this, "密码长度应该在6-15位");
            return;
        }
        String reNewPwd = etReNewPwd.getText().toString().trim();
        if (!newPwd.equals(reNewPwd)){
            ToastUtils.showToast(this, "两次新密码不一致,请重新输入密码");
            return;
        }
        LoadDialog.show(PasswordSetActivity.this, "修改中...");
        new UpdatePersonInfoPest(this).updatePassword(this, oldPwd, newPwd, reNewPwd);
    }

    @Override
    public void showError(String message) {
        LoadDialog.dismiss(PasswordSetActivity.this);
        ToastUtils.showToast(PasswordSetActivity.this, message);
    }

    private IDataStorage dataStorage;
    @Override
    public void updatePersonSuccess(UserBean userBeans) {
        LoadDialog.dismiss(PasswordSetActivity.this);
        ToastUtils.showToast(PasswordSetActivity.this, "修改成功");
        dataStorage = DataStorageFactory.getInstance(
                this, DataStorageFactory.TYPE_DATABASE);
        if (dataStorage.contains(UserBean.class, getPackageName())) {
            UserBean userBean = dataStorage.load(UserBean.class, getPackageName());
            userBean.setPassword(newPwd);
            dataStorage.storeOrUpdate(userBean, getPackageName());
        }
        finish();
    }
}
